package com.yunchang.springboot.scheduled.amazon.mws.report.datadispose;

import com.yunchang.springboot.mapper.mwsreport.IMwsInventoryAdjustmentsMapper;
import com.yunchang.springboot.models.mwsreportbean.MwsInventoryAdjustments;
import com.yunchang.springboot.models.mwsreportbean.MwsInventoryAdjustmentsDO;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.parent.ReportDataDisposeParent;
import com.yunchang.springboot.scheduled.amazon.mws.report.datadispose.reportinterface.IReportDataDispose;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.StringUtil;
import com.yunchang.springboot.utils.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author czw
 * @date 2020/7/28 0028  18:50
 * @description:
 */
@Component
public class InventoryDataDispose extends ReportDataDisposeParent implements IReportDataDispose {

    @Autowired
    IMwsInventoryAdjustmentsMapper mwsInventoryAdjustmentsMapper;
    /**
     * 盘库 将获取到的数据转为实体对象数组
     *
     * @param lists
     * @param shopBrandName
     * @param area
     * @return
     * @throws Exception
     */
    @Override
    public List<Object> reportDataDispose(List<List<String>> lists, String shopBrandName, String area,String sellerId) throws Exception {
        List<String> titleList = lists.get(0);
        Map<String, Object> titleMap = new HashMap<>(titleList.size());
        for (int i = 0; i < titleList.size(); i++) {
            titleMap.put(titleList.get(i), i);
        }
        int adjustedDateIndex = Integer.valueOf(String.valueOf(titleMap.get("adjusted-date")));
        String latestDay = mwsInventoryAdjustmentsMapper.getLatestDay(sellerId, area);
        if (!StringUtil.isBlank(latestDay)) {
            List<String> firstList = lists.get(1);
            if (DateUtil.compateTime(latestDay, DateUtil.ISO8601ToGMT(firstList.get(adjustedDateIndex))) >= 0) {
                return null;
            }
        }
        int transactionItemIdIndex = Integer.valueOf(String.valueOf(titleMap.get("transaction-item-id")));
        int fnskuIndex = Integer.valueOf(String.valueOf(titleMap.get("fnsku")));
        int skuIndex = Integer.valueOf(String.valueOf(titleMap.get("sku")));
        int productNameIndex = Integer.valueOf(String.valueOf(titleMap.get("product-name")));
        int fulfillmentCenterIdIndex = Integer.valueOf(String.valueOf(titleMap.get("fulfillment-center-id")));
        int quantityIndex = Integer.valueOf(String.valueOf(titleMap.get("quantity")));
        int reasonIndex = Integer.valueOf(String.valueOf(titleMap.get("reason")));
        int dispositionIndex = Integer.valueOf(String.valueOf(titleMap.get("disposition")));
        int reconciledIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("reconciled")) {
            reconciledIndex = Integer.valueOf(String.valueOf(titleMap.get("reconciled")));
        }
        int unreconciledIndex = WRONG_NUMBER_SIGN;
        if (null != titleMap.get("unreconciled")) {
            unreconciledIndex = Integer.valueOf(String.valueOf(titleMap.get("unreconciled")));
        }

        List<Object> list = new ArrayList<>(lists.size());
        for (int i = 1; i < lists.size(); i++) {
            MwsInventoryAdjustmentsDO mwsInventoryAdjustments = new MwsInventoryAdjustmentsDO();
            List<String> row = lists.get(i);
            mwsInventoryAdjustments.setSellerId(sellerId);
            mwsInventoryAdjustments.setArea(area);
            mwsInventoryAdjustments.setAdjustedDate(DateUtil.ISO8601ToGMT(row.get(adjustedDateIndex)));
            mwsInventoryAdjustments.setTransactionItemId(row.get(transactionItemIdIndex));
            mwsInventoryAdjustments.setFnsku(row.get(fnskuIndex));
            mwsInventoryAdjustments.setSku(row.get(skuIndex));
            mwsInventoryAdjustments.setProductName(row.get(productNameIndex));
            mwsInventoryAdjustments.setFulfillmentCenterId(row.get(fulfillmentCenterIdIndex));
            mwsInventoryAdjustments.setQuantity(ToolUtil.valueOfInteger(row.get(quantityIndex)));
            mwsInventoryAdjustments.setReason(row.get(reasonIndex));
            mwsInventoryAdjustments.setDisposition(row.get(dispositionIndex));
            mwsInventoryAdjustments.setCreateTime(DateUtil.getDate());
            mwsInventoryAdjustments.setUpdateTime(DateUtil.getDate());
            if (reconciledIndex != WRONG_NUMBER_SIGN) {
                mwsInventoryAdjustments.setReconciled(ToolUtil.valueOfInteger(row.get(reconciledIndex)));
            }
            if (unreconciledIndex != WRONG_NUMBER_SIGN) {
                mwsInventoryAdjustments.setUnreconciled(ToolUtil.valueOfInteger(row.get(unreconciledIndex)));
            }
            mwsInventoryAdjustments.setFinanceStatus(1);
            list.add(mwsInventoryAdjustments);
        }
        return list;
    }
}
